package com.dogborther.pojo;

import com.dogborther.utils.ChopstickStatus;
import com.dogborther.utils.PhilosopherStatus;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.net.URL;
import java.util.Random;

import static com.dogborther.utils.TimeConstant.*;

@NoArgsConstructor
@Data
public class Mode3Philosopher extends Philosopher{
    private int id;

    private Button button;

    private ImageView imageView;
    private ImageView leftHandChopstick;
    private ImageView rightHandChopstick;

    private Chopstick leftChopstick;
    private Chopstick rightChopstick;
    public static Integer selectMode;
    public static boolean stop = true;
    public static Integer roomMute =4;
    public void setImage(PhilosopherStatus philosopherStatus){
        URL resource = Chopstick.class.getResource(philosopherStatus.getImagePath());
        Image image = new Image(resource.toExternalForm(),200,200,true,true,true);
        imageView.setImage(image);
    }

    public void run(){
        System.out.println("mode3:开始");
        think();
        hungry();
        waitRoom();
        takeChopstickFromLeftToRight();
        dropDownChopsticks();
        outRoom();
    }

    private void outRoom() {
        roomMute++;
    }

    private void waitRoom() {
        while(roomMute <=0){
            System.out.println(id+":满人了，我在等着呢");
            try {
                Thread.sleep(WAIT_ROOM_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        roomMute--;
    }

    public void think() {
        this.setImage(PhilosopherStatus.THINKING);
        try {
            Thread.sleep(getRandomThinkTime());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void eat(){
        this.setImage(PhilosopherStatus.EATING);
    }
    public void hungry(){
        this.setImage(PhilosopherStatus.HUNGRY);
    }
    public void takeChopstickFromLeftToRight(){
        synchronized (leftChopstick){
            leftChopstick.setImage(ChopstickStatus.TAKE_UP);
            leftChopstick.getImageView().setVisible(false);
            leftHandChopstick.setVisible(true);
            try {
                Thread.sleep(WAIT_NEXT_CHOPSTICK_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (rightChopstick){
                rightChopstick.setImage(ChopstickStatus.TAKE_UP);
                rightChopstick.getImageView().setVisible(false);
                rightHandChopstick.setVisible(true);
                eat();
                try {
                    Thread.sleep(getRandomEatingTime());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.setImage(PhilosopherStatus.THINKING);
            }
        }
    }
    public void takeChopstickFromRightToLeft(){
        synchronized (rightChopstick){
            rightChopstick.setImage(ChopstickStatus.TAKE_UP);
            rightChopstick.getImageView().setVisible(false);
            rightHandChopstick.setVisible(true);
            try {
                Thread.sleep(WAIT_NEXT_CHOPSTICK_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (leftChopstick){
                leftChopstick.setImage(ChopstickStatus.TAKE_UP);
                leftChopstick.getImageView().setVisible(false);
                leftHandChopstick.setVisible(true);
                eat();
                try {
                    Thread.sleep(getRandomEatingTime());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.setImage(PhilosopherStatus.THINKING);
            }
        }
    }
    private synchronized void dropDownChopsticks() {
        this.leftHandChopstick.setVisible(false);
        this.rightHandChopstick.setVisible(false);
        this.leftChopstick.getImageView().setVisible(true);
        this.rightChopstick.getImageView().setVisible(true);
        leftChopstick.setImage(ChopstickStatus.DROP_DOWN);
        rightChopstick.setImage(ChopstickStatus.DROP_DOWN);
    }

}
